<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Application extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();

        $this->load->library('template');
        $this->template->set_template('backend');
        $this->load->library('carabiner');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->model('application_model');
        
        if (!is_loggedin()) {
            redirect($this->router->reverseRoute('backend_login') . '?redirect=' . base64_encode(current_url()));
        }
    }

    function index() {

        $this->template->write_view('side_menu', 'backend/side_menu');
        $this->template->write_view('content', 'backend/application/application_list');
        $this->template->render();
    }
    
    function create() {
        $form_data['form_url'] = base_url('backend/application/create/');   
        $this->template->write_view('side_menu', 'backend/side_menu');
        $this->template->write_view('content', 'backend/application/application_form', $form_data);
        $this->template->render();
    }
    
    function edit($application_id=0) {
                
        
        if ($_POST) {
            
            $this->load->library('form_validation');
            $this->form_validation->set_rules('cdt_rank', 'cdt_rank', 'trim');
            if ($this->form_validation->run() === TRUE) {
                
                $admission_test  = implode('|',$this->input->post('admission_test'));
                $sources  = implode('|',$this->input->post('sources'));
                
                $required_doc  = implode('|',$this->input->post('required_doc'));
                $required_doc_staff  = implode('|',$this->input->post('required_doc_staff'));
                
                $update_data = array(
                    'cdt_rank' => $this->input->post('cdt_rank'),
                    'did_rank' => $this->input->post('did_rank'),
                    'isc_rank' => $this->input->post('isc_rank'),
                    'name_en' => $this->input->post('name_en'),
                    'name_th' => $this->input->post('name_th'),
                    'birthday' => $this->input->post('birthday'),
                    'nationality' => $this->input->post('nationality'),
                    'gender' => $this->input->post('gender'),
                    'religion' => $this->input->post('religion'),
                    'citizenship' => $this->input->post('citizenship'),
                    'citizen_id' => $this->input->post('citizen_id'),
                    'citizenship_of' => $this->input->post('citizenship_of'),
                    'passport_no' => $this->input->post('passport_no'),
                    'permanent_address' => $this->input->post('permanent_address'),
                    'telephone' => $this->input->post('telephone'),
                    'mobilephone' => $this->input->post('mobilephone'),
                    'mailing_address' => $this->input->post('mailing_address'),   
                    'telephone2' => $this->input->post('telephone2'),
                    'email' => $this->input->post('email'),
                    'father_name' => $this->input->post('father_name'),
                    'father_occupation' => $this->input->post('father_occupation'),
                    'father_company' => $this->input->post('father_company'),
                    'father_telephone' => $this->input->post('father_telephone'),
                    'father_email' => $this->input->post('father_email'),                    
                    'mother_name' => $this->input->post('mother_name'),
                    'mother_occupation' => $this->input->post('mother_occupation'),
                    'mother_company' => $this->input->post('mother_company'),
                    'mother_telephone' => $this->input->post('mother_telephone'),
                    'mother_email' => $this->input->post('mother_email'),                    
                    'guardian_name' => $this->input->post('guardian_name'),
                    'guardian_occupation' => $this->input->post('guardian_occupation'),
                    'guardian_company' => $this->input->post('guardian_company'),
                    'guardian_telephone' => $this->input->post('guardian_telephone'),
                    'guardian_email' => $this->input->post('guardian_email'),
                    
                    'academic_level_1' => $this->input->post('academic_level_1'),                    
                    'academic_language_1' => $this->input->post('academic_language_1'),
                    'academic_institution_1' => $this->input->post('academic_institution_1'),
                    'academic_major_1' => $this->input->post('academic_major_1'),
                    'academic_gpa_1' => $this->input->post('academic_gpa_1'),
                    'academic_from_1' => $this->input->post('academic_from_1'),
                    'academic_to_1' => $this->input->post('academic_to_1'),
                    
                    'academic_level_2' => $this->input->post('academic_level_2'),                    
                    'academic_language_2' => $this->input->post('academic_language_2'),
                    'academic_institution_2' => $this->input->post('academic_institution_2'),
                    'academic_major_2' => $this->input->post('academic_major_2'),
                    'academic_gpa_2' => $this->input->post('academic_gpa_2'),
                    'academic_from_2' => $this->input->post('academic_from_2'),
                    'academic_to_2' => $this->input->post('academic_to_2'),
                    
                    'academic_level_3' => $this->input->post('academic_level_3'),                    
                    'academic_language_3' => $this->input->post('academic_language_3'),
                    'academic_institution_3' => $this->input->post('academic_institution_3'),
                    'academic_major_3' => $this->input->post('academic_major_3'),
                    'academic_gpa_3' => $this->input->post('academic_gpa_3'),
                    'academic_from_3' => $this->input->post('academic_from_3'),
                    'academic_to_3' => $this->input->post('academic_to_3'),
                    
                    'awards' => $this->input->post('awards'),
                    'activities' => $this->input->post('activities'),
                    
                    'gpa_opt_1' => $this->input->post('gpa_opt_1'),                    
                    'gpa_opt_1_gpa' => $this->input->post('gpa_opt_1_gpa'),
                    'gpa_opt_2' => $this->input->post('gpa_opt_2'),
                    'gpa_opt_2_gpa' => $this->input->post('gpa_opt_2_gpa'),
                    'gpa_opt_2_math' => $this->input->post('gpa_opt_2_math'),
                    'gpa_opt_2_phy' => $this->input->post('gpa_opt_2_phy'),
                    
                    'is_standard_test' => $this->input->post('is_standard_test'),
                    'english_test' => $this->input->post('english_test'),
                    'english_test_base' => $this->input->post('english_test_base'),
                    
                    'english_test_overall' => $this->input->post('english_test_overall'),
                    'english_test_writing' => $this->input->post('english_test_writing'),
                    'english_test_reading' => $this->input->post('english_test_reading'),
                    'english_test_speaking' => $this->input->post('english_test_speaking'),
                    'english_test_listening' => $this->input->post('english_test_listening'),
                    'english_test_date' => $this->input->post('english_test_date'),
                    
                    'science_test' => $this->input->post('science_test'),
                    'science_test_overall' => $this->input->post('science_test_overall'),
                    'science_test_math' => $this->input->post('science_test_math'),
                    'science_test_phy' => $this->input->post('science_test_phy'),
                    'science_test_chem' => $this->input->post('science_test_chem'),
                    'science_test_bio' => $this->input->post('science_test_bio'),
                    'science_test_date' => $this->input->post('science_test_date'),
                    
                    'admission_test' => $admission_test,
                    'admission_test_onet' => $this->input->post('admission_test_onet'),
                    'admission_test_onet_date' => $this->input->post('admission_test_onet_date'),
                    'admission_test_anet' => $this->input->post('admission_test_anet'),
                    'admission_test_anet_date' => $this->input->post('admission_test_anet_date'),
                    'admission_test_gat' => $this->input->post('admission_test_gat'),
                    'admission_test_gat_date' => $this->input->post('admission_test_gat_date'),
                    'admission_test_pati' => $this->input->post('admission_test_pati'),
                    'admission_test_pati_date' => $this->input->post('admission_test_pati_date'),
                    'admission_test_patii' => $this->input->post('admission_test_patii'),
                    'admission_test_patii_date' => $this->input->post('admission_test_patii_date'),
                    
                    'sources' => $sources,
                    'sources_specify_1' => $this->input->post('sources_specify_1'),
                    'sources_specify_2' => $this->input->post('sources_specify_2'),
                    'sources_specify_3' => $this->input->post('sources_specify_3'),
                    'sources_specify_4' => $this->input->post('sources_specify_4'),
                    'sources_specify_5' => $this->input->post('sources_specify_5'),
                    
                    'another_school_1' => $this->input->post('another_school_1'),
                    'another_school_2' => $this->input->post('another_school_2'),
                    'another_school_3' => $this->input->post('another_school_3'),
                    'another_school_4' => $this->input->post('another_school_4'),
                    
                    'suggestions' => $this->input->post('suggestions'),
                    
                    'required_doc' => $required_doc,
                    'required_doc_staff' => $required_doc_staff,
                    
                    'payment_type' => $this->input->post('payment_type')
                        
                );
                
                $update_data['application_no'] = $this->input->post('application_no');               
                
                
                $this->application_model->edit($application_id, $update_data);
                redirect('backend/application/edit/'.$application_id);
                
            } else {
                echo validation_errors();
                exit;
            }
        } else {
                                 
            
            $form_data = $this->application_model->get($application_id);
            $form_data['form_url'] = base_url('backend/application/edit/'.$application_id);   
            
            $this->template->write_view('side_menu', 'backend/side_menu');
            $this->template->write_view('content', 'backend/application/application_form',$form_data);
            $this->template->render();
            return TRUE;
        }
    }
    
    
    function json_data($role = '') {


        $this->load->library('Datatables');

        if (is_admin()) {
            $buttons = '<a class="btn btn-default pull-right tbl-delete-row" href="#" rel="' . base_url('backend/application/delete') . '/$1" ><i class="icon-remove"></i></a>';
            $buttons .= '<a class="btn btn-default pull-right" href="' . base_url('backend/application/edit') . '/$1" rel="$1"  ><i class="icon-edit"></i></a>';
        }else{
            $buttons = '<a class="btn btn-default pull-right tbl-delete-row disable" href="#" ><i class="icon-remove"></i></a>';
            $buttons .= '<a class="btn btn-default pull-right disable" href="#" rel="$1"  ><i class="icon-edit"></i></a>';
        }
        
        $this->datatables
                ->select('application_id, name_th,telephone,email')
                ->from('applications')
                ->unset_column('members.member_lastname')
                ->add_column('Tools', $buttons, 'application_id');

        echo $this->datatables->generate();
    }

}
